<?php namespace app\common\model;
use think\Loader;
use think\Validate;
use think\Db;
use houdunwang\arr\Arr;
//权限表模型
class AuthRule extends Base{
	//添加数据
	public function addData($data){
		
		$validate = Loader::validate('AuthRule');
			
			if(!$validate->check($data)){
			    return ['valid'=>0,'msg'=>$validate->getError()];
			}else{
				$data['name']=trim($data['name']);
				$data['title']=trim($data['title']);
				Db::name('auth_rule')->insert($data);
                $userId = Db::name('auth_rule')->getLastInsID();
				if($userId){
					 return ['valid'=>1,'msg'=>'权限添加成功！'];
				}else{
					 return ['valid'=>0,'msg'=>'权限添加失败！'];
				}
			}
	}
	
	//添加数据
	public function editData($data){
		
		$validate = Loader::validate('AuthRule');
			
			if(!$validate->check($data)){
			    return ['valid'=>0,'msg'=>$validate->getError()];
			}else{
//				halt($data);
                $data['name']=trim($data['name']);
				$data['title']=trim($data['title']);
				$userId=db('auth_rule')
   				 ->where('id',$data['id'])
                 ->update($data);
				if($userId){
					 return ['valid'=>1,'msg'=>'权限修改成功！'];
				}else{
					 return ['valid'=>0,'msg'=>'权限修改失败！'];
				}
			}
	}
	
 	 /**
     * 获取全部数据
     * @param  string $type  tree获取树形结构 level获取层级结构
     * @param  string $order 排序方式   
     * @return array         结构数据
     */
	 public function getTreeData($type='tree',$name='title',$child='id',$parent='pid'){
//      // 判断是否需要排序
//      if(empty($order)){
//          $data=db('auth_rule')->select();
//      }else{
//          $data=db('auth_rule')->order($order.' is null,'.$order)->select();
//      }
        // 获取树形或者结构数据
        if($type=='tree'){
//           $data=\Org\Nx\Data::tree($data,$name,$child,$parent);
             $data=Arr::tree(db('auth_rule')->select(),$name, $child , $parent );
        }elseif($type="level"){
//          $data=\Org\Nx\Data::channelLevel($data,0,'&nbsp;',$child);
			$data=Arr::channelList(db('auth_rule')->select(), $pid = 0, $html = "&nbsp;", $child, $parent);
        }
        return $data;
    }
}


